package org.infodavid.common.text;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.Iterator;
import java.util.Random;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * The Class TextUtil.
 */
public final class TextUtil {

  /** logger as Logger. */
  private static final Log LOGGER = LogFactory.getLog(TextUtil.class);

  /** The Constant isDebugEnabled. */
  private static final boolean isDebugEnabled = LOGGER.isDebugEnabled();

  /** The rand. */
  private static Random rand = new Random(System.currentTimeMillis());

  /**
   * Generate random string.
   * @param characters the characters
   * @param length the length
   * @return the string
   */
  public static String generateRandomString(final String characters, final int length) {
    final StringBuffer sb = new StringBuffer(length);

    for (int i = 0; i < length; i++) {
      final int pos = rand.nextInt(characters.length());
      sb.append(characters.charAt(pos));
    }

    return sb.toString();
  }

  /**
   * Gets the boolean.
   * @param value the value
   * @return the boolean
   */
  public static boolean toBoolean(final String value) {
    return "1".equals(value) || "on".equalsIgnoreCase(value) || "ok".equalsIgnoreCase(value)
        || "yes".equalsIgnoreCase(value) || Boolean.parseBoolean(value);
  }

  /**
   * Gets the boolean.
   * @param value the value
   * @param defaultValue the default value
   * @return the boolean
   */
  public static boolean toBoolean(final String value, final boolean defaultValue) {
    boolean b;

    if (value == null) {
      b = defaultValue;
    }
    else {
      try {
        b = toBoolean(value);
      }
      catch (final NumberFormatException e) {
        b = defaultValue;
      }
    }

    return b;
  }

  /**
   * F Gets the byte.
   * @param value the value
   * @return the byte
   */
  public static byte toByte(final String value) {
    return (byte)Double.parseDouble(value);
  }

  /**
   * Gets the byte.
   * @param value the value
   * @param defaultValue the default value
   * @return the byte
   */
  public static byte toByte(final String value, final byte defaultValue) {
    byte b;

    if (value == null) {
      b = defaultValue;
    }
    else {
      try {
        b = toByte(value);
      }
      catch (final NumberFormatException e) {
        b = defaultValue;
      }
    }

    return b;
  }

  /**
   * Gets the double.
   * @param value the value
   * @return the double
   */
  public static double toDouble(final String value) {
    return Double.parseDouble(value);
  }

  /**
   * Gets the double.
   * @param value the value
   * @param defaultValue the default value
   * @return the double
   */
  public static double toDouble(final String value, final double defaultValue) {
    double d;

    if (value == null) {
      d = defaultValue;
    }
    else {
      try {
        d = toDouble(value);
      }
      catch (final NumberFormatException e) {
        d = defaultValue;
      }
    }

    return d;
  }

  /**
   * Gets the float.
   * @param value the value
   * @return the float
   */
  public static float toFloat(final String value) {
    return (float)Double.parseDouble(value);
  }

  /**
   * Gets the float.
   * @param value the value
   * @param defaultValue the default value
   * @return the float
   */
  public static float toFloat(final String value, final float defaultValue) {
    float f;

    if (value == null) {
      f = defaultValue;
    }
    else {
      try {
        f = toFloat(value);
      }
      catch (final NumberFormatException e) {
        f = defaultValue;
      }
    }

    return f;
  }

  /**
   * Gets the integer.
   * @param value the value
   * @return the integer
   */
  public static int toInt(final String value) {
    return (int)Double.parseDouble(value);
  }

  /**
   * Gets the integer.
   * @param value the value
   * @param defaultValue the default value
   * @return the integer
   */
  public static int toInt(final String value, final int defaultValue) {
    int i;

    if (value == null) {
      i = defaultValue;
    }
    else {
      try {
        i = toInt(value);
      }
      catch (final NumberFormatException e) {
        i = defaultValue;
      }
    }

    return i;
  }

  /**
   * Gets the long.
   * @param value the value
   * @return the long
   */
  public static long toLong(final String value) {
    return (long)Double.parseDouble(value);
  }

  /**
   * Gets the long.
   * @param value the value
   * @param defaultValue the default value
   * @return the long
   */
  public static long toLong(final String value, final long defaultValue) {
    long l;

    if (value == null) {
      l = defaultValue;
    }
    else {
      try {
        l = toLong(value);
      }
      catch (final NumberFormatException e) {
        l = defaultValue;
      }
    }

    return l;
  }

  /**
   * Gets the short.
   * @param value the value
   * @return the short
   */
  public static short toShort(final String value) {
    return (short)Double.parseDouble(value);
  }

  /**
   * Gets the short.
   * @param value the value
   * @param defaultValue the default value
   * @return the short
   */
  public static short toShort(final String value, final short defaultValue) {
    short s;

    if (value == null) {
      s = defaultValue;
    }
    else {
      try {
        s = toShort(value);
      }
      catch (final NumberFormatException e) {
        s = defaultValue;
      }
    }

    return s;
  }

  /**
   * Gets the property value.
   * @param property the property
   * @param defaultValue the default value
   * @return the property value
   */
  public static String toString(final String property, final String defaultValue) {
    final String strValue = System.getProperty(property);
    String value;

    if (StringUtils.isEmpty(strValue)) {
      if (isDebugEnabled) {
        LOGGER.debug("Using default value for property: " + property);
      }

      value = defaultValue;
    }
    else {
      value = strValue.trim().toLowerCase();
    }

    if (isDebugEnabled) {
      LOGGER.debug("Value for property: " + property + " is " + value);
    }

    return value;
  }

  /**
   * To string.
   * @param <T> the generic type
   * @param separator the separator
   * @param buffer the buffer
   * @param values the values
   */
  @SuppressWarnings("rawtypes")
  public static <T extends Object> void toString(final String separator, final StringBuffer buffer,
      final Collection<T> values) {
    if (!(values == null || values.isEmpty())) {
      final Iterator ite = values.iterator();
      buffer.append(ite.next());

      while (ite.hasNext()) {
        buffer.append(separator);
        buffer.append(ite.next());
      }
    }
  }

  /**
   * To string.
   * @param <T> the generic type
   * @param separator the separator
   * @param buffer the buffer
   * @param values the values
   */
  @SafeVarargs
  public static <T extends Object> void toString(final String separator, final StringBuffer buffer,
      final T... values) {
    if (!(values == null || values.length == 0)) {
      buffer.append(values[0]);

      for (int i = 1; i < values.length; i++) {
        buffer.append(separator);
        buffer.append(values[i]);
      }
    }
  }

  /**
   * To string.
   * @param <T> the generic type
   * @param separator the separator
   * @param values the values
   * @return the string
   */
  @SafeVarargs
  public static <T extends Object> String toString(final String separator, final T... values) {
    final StringBuffer buffer = new StringBuffer();

    toString(separator, buffer, values);

    return buffer.toString();
  }

  /**
   * Gets the URL.
   * @param value the value
   * @return the URL
   * @throws MalformedURLException the malformed url exception
   */
  public static URL toURL(final String value) throws MalformedURLException {
    return value == null ? null : new URL(value);
  }

  /**
   * The Constructor.
   */
  private TextUtil() {}
}
